package intro;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

/**
 * 查看一个Java程序包含哪些线程
 * 
 * @author insight
 * @since 2021/8/23
 */
public class GetThreadInfo {
    public static void main(String[] args) {
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        // 忽略同步的 monitor & synchronizer
        ThreadInfo[] threadInfos = threadBean.dumpAllThreads(false, false);

        /*
            [1]main
            [2]Reference Handler
            [3]Finalizer
            [4] Signal Dispatcher
            [5]Attach Listener
            [11]Common-Cleaner
            [12] Monitor Ctrl-Break
        */
        for (ThreadInfo threadInfo : threadInfos) {
            System.out.println("[" + threadInfo.getThreadId() + "]" + threadInfo.getThreadName());
        }
    }
}